package com.bhs.sansong.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.bean.ActivityBean;
import com.bhs.sansong.bean.DiscountCouponBean;
import com.bhs.sansong.bean.PictureBean;
import com.bhs.sansong.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券列表（新人、活动）
 */
public class DiscountCouponDialog extends Dialog implements View.OnClickListener {

    private TextView mTvDivision;
    private RecyclerView mRvList;
    private TextView mTvImmediateClaim;
    private ImageView mIvClose;
    private BaseQuickAdapter<DiscountCouponBean, BaseViewHolder> mAdapter;
    private TextView mTvNumber;
    private List<DiscountCouponBean> list;
    private String typeName;

    public DiscountCouponDialog(@NonNull Context context, List<DiscountCouponBean> list, String typeName) {
        super(context, R.style.MyBottomDialogStyle);
        this.list = list;
        this.typeName = typeName;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_discount_coupon);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = MyUtils.getScreenWidth(getContext()) * 8 / 10;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        setCanceledOnTouchOutside(false);
        initView();
        mTvNumber.setText(String.format("获得%s张优惠券", list.size()));
        mTvDivision.setText(typeName);
    }

    private void initView() {
        mTvDivision = findViewById(R.id.tv_division);
        mRvList = findViewById(R.id.rv_list);
        mTvImmediateClaim = findViewById(R.id.tv_Immediate_claim);
        mIvClose = findViewById(R.id.iv_close);
        mIvClose.setOnClickListener(this);
        mTvImmediateClaim.setOnClickListener(this);
        mAdapter = new BaseQuickAdapter<DiscountCouponBean, BaseViewHolder>(R.layout.item_activity, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, DiscountCouponBean item) {
                helper.setText(R.id.tv_type, String.format("平台%s券", item.getUse_type()));
                helper.setText(R.id.tv_full_subtraction, String.format("满%s使用", item.getCoupon_min_price()));
                helper.setText(R.id.tv_bond_price, MyUtils.checkPoint(item.getCoupon_price()));
                helper.setText(R.id.tv_time, String.format("有效期至%s", item.getDate_end()));
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mTvNumber = findViewById(R.id.tv_number);
    }

    @Override
    public void onClick(View view) {
        dismiss();
        if (view.getId() == R.id.tv_Immediate_claim) {
            listener.onClick(CommaSplice(), "立即领取");
        } else {
            listener.onClick(CommaSplice(), null);
        }
    }

    private MyClickListener2 listener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.listener = l;
    }

    public String CommaSplice() {
        StringBuilder images = new StringBuilder();
        for (DiscountCouponBean mDatum : list) {
            images.append(mDatum.getId()).append(",");

        }
        if (!TextUtils.isEmpty(images)) {
            images = new StringBuilder(images.substring(0, images.toString().lastIndexOf(",")));
        }
        return images.toString();
    }
}
